package view.Etudiants.Accueil;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import Controller.ExerciceController;

import view.Fenetre;
import view.Param;
import view.Accueil.LogPanel;
import view.Etudiants.Page.EtudPage;

@SuppressWarnings("serial")
public class CenterPanel extends JPanel implements ActionListener {

	private ArrayList<JButton> bList;

	public CenterPanel() {

		// Agencement des conteneurs
		GridBagConstraints constraint = new GridBagConstraints();
		this.setLayout(new GridBagLayout());

		// Creation des conteneurs
		bList = new ArrayList<JButton> ();
		JLabel lTitre = new JLabel("Veuillez choisir votre th\u00E8me :");

		// Ajout de la transparence
		this.setOpaque(false);

		ExerciceController exc = Fenetre.control.getExerciceController();
		ArrayList<String> listeTheme = exc.listeTheme();

		constraint.gridx = 1;
		constraint.gridy = 0;

		constraint.weightx = 1.0;
		constraint.weighty = 1/listeTheme.size();

		constraint.gridwidth = 1;
		constraint.gridheight = 1;

		constraint.fill = GridBagConstraints.CENTER;

		this.add(lTitre, constraint);

		for (int i=0 ; i<listeTheme.size(); i++)
		{
			JButton courant = new JButton(listeTheme.get(i));
			bList.add(courant);

			// Ajout des ecouteurs aux boutons
			courant.addActionListener(this);

			constraint.gridy += 1;

			this.add(courant,constraint);
		}

		Border margin = BorderFactory.createEmptyBorder(0, 0, 10, 0);		// top, left, bottom, right
		lTitre.setBorder(margin);

		// Ajout des marges
		this.setBorder(Param.wideMargin);

	}

	@Override
	public void actionPerformed(ActionEvent event) {

		LogPanel.wEtudAccueil.erase();

		for (int i=0; i<bList.size(); i++) {

			JButton bCourant = bList.get(i);

			if (event.getSource() == bCourant) {

				EtudPage.wEtud = new EtudPage(bCourant.getText());

			}

		}

	}

}
